<?php

class userWu_UserTypeController extends Zend_Controller_Action
{

	
    public function init()
    {
        /* Initialize action controller here */
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());
    }

    public function indexAction()
    {
    	$this->_helper->layout()->disableLayout();	
    	$tr = Application_Form_FrmLanguages::getCurrentlanguage();
        $db = new userWu_Model_DbTable_DbUserType();
        $userQuery = "SELECT u.user_type_id,u.user_type,
        		(SELECT u1.user_type FROM `rsv_acl_user_type` as u1 WHERE u1.user_type_id = u.parent_id LIMIT 1) as  parent_id
        		FROM `rsv_acl_user_type` as u where u.user_type_id !=1";
        $rows = $db->getUserTypeInfo($userQuery);
        $this->view->formusertype = $rows;
        if($this->getRequest()->isPost()){
        	$id = $this->getRequest()->getPost();
        	//print_r($id);exit();
        	if($this->getRequest()->getPost("status")){
        		$id = $this->getRequest()->getParam("status");
        		//print_r($id);exit();
        		if(!empty($id)){
        			$db->updateUnStatus($id);
        			//echo "<script type='text/javascript'> alert('Update Status Faculty Successfull'); </script>";
        		}
        		//$this->_redirect("faculty/fac/index");
        	}
        	if($this->getRequest()->getPost("unstatus")){
        		$id = $this->getRequest()->getParam("unstatus");
        		//print_r($id);exit();
        		$db->updateStatus($id);
        		//echo "<script type='text/javascript'> alert('Update Status Faculty Successfull'); </script>";
        	}
        }
    }
    
    public function viewUserTypeAction()
    {   
    	/* Initialize action controller here */
    	if($this->getRequest()->getParam('id')){
    		$db = new userWu_Model_DbTable_DbUserType();
    		$user_type_id = $this->getRequest()->getParam('id');
    		$rs=$db->getUserType($user_type_id);
    		$this->view->rs=$rs;
    	}
    }
	public function addUserTypeAction()
		{
			$form=new userWu_Form_FrmUserType();
			$this->view->form=$form;
			$tr = Application_Form_FrmLanguages::getCurrentlanguage();
			if($this->getRequest()->isPost())
			{
				$db=new userWu_Model_DbTable_DbUserType();	
				$post=$this->getRequest()->getPost();
				if(!$db->isUserTypeExist($post['user_type'])){
					$id=$db->insertUserType($post);
// 					$tr = Application_Form_FrmLanguages::getCurrentlanguage();
// 					Application_Form_FrmMessage::message($tr->translate('ROW_AFFECTED'));
// 					Application_Form_FrmMessage::redirector('/rsvAcl/user-type/index');
					$this->_redirect('/userWu/user-type/index');
				}else {
					Application_Form_FrmMessage::message('User type had existed already');
				}
			}
		}
    public function editUserTypeAction()
    {	
    	$user_type_id=$this->getRequest()->getParam('id');
    	if(!$user_type_id)$user_type_id=0;

   		$form = new userWu_Form_FrmUserType();

    	$db = new userWu_Model_DbTable_DbUserType();
        $rs = $db->getUserTypeInfo('SELECT * FROM rsv_acl_user_type where user_type_id='.$user_type_id);
		Application_Model_Decorator::setForm($form, $rs);

    	$this->view->form = $form;
    	$this->view->user_id = $user_type_id;
    	$tr = Application_Form_FrmLanguages::getCurrentlanguage();
    	if($this->getRequest()->isPost())
		{
			$post=$this->getRequest()->getPost();
			if($rs[0]['user_type']==$post['user_type']){
				Application_Form_FrmMessage::message($tr->translate('ROW_AFFECTED'));
				$db->updateUserType($post,$rs[0]['user_type_id']);
				Application_Form_FrmMessage::redirector('/userWu/user-type/index');
			}else{
				if(!$db->isUserTypeExist($post['user_type'])){
					$db->updateUserType($post,$rs[0]['user_type_id']);
					Application_Form_FrmMessage::message($tr->translate('ROW_AFFECTED'));
					Application_Form_FrmMessage::redirector('/userWu/user-type/index');
				}else {
					Application_Form_FrmMessage::message('User had existed already');
				}
			}
		}
    }
}